package com.cn.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: demo
 * @description: 生成随机姓名
 * @author: yihanyuan
 * @create: 2019-05-31 15:23
 */
public class RandomName {
    public static void main(String[] args) throws IOException {
        for (int i = 50; i-- > 0; ) {
            String name = firstName() + secondName(null);
            System.out.println(name);
        }
    }

    public final static String getName(String sex) throws IOException {
        return firstName() + secondName(sex);
    }

    /*
     * 随机返回a和b其中一个数
     */
    public static int randomAB(int a, int b) {
        return (int) ((Math.random() * Math.abs(a - b)) + Math.min(a, b));
    }

    /**
     * 生成姓氏
     *
     * @throws IOException
     */
    private static String firstName() throws IOException {
        List<String> fistNames = loadFile("/name/百家姓");
        return fistNames.get(randomAB(0, fistNames.size()));
    }

    /**
     * 读取姓氏文件，获取姓氏
     *
     * @return
     * @throws IOException
     */
//    private static List<String> loadBaiJiaXing(String path) throws IOException {
//        //使用类加载器来加载文件
//        InputStream in = RandomName.class.getResourceAsStream(path);
//        BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
//        //文件读取
//        String line = null;
//        //结果集合
//        List<String> result = new ArrayList<>(200);
//        while ((line = br.readLine()) != null) {
//            line = line.trim();
//            //使用空白字符分割
//            String[] names = line.split("\\s+");
//            result.addAll(Arrays.asList(names));
//        }
//        return result;
//    }

    /**
     * @return
     * @Author yihanyuan
     * @Description 输入性别，返回对应男生名字或者女生名字，不输则随机
     * @Date 15:35 2019/5/31
     * @Param
     **/


    private static String secondName(String sex) throws IOException {
        if ("boy".equals(sex)) {
            List<String> names = loadFile("/name/男性");
            return names.get(randomAB(0, names.size()));
        } else if ("girl".equals(sex)) {
            List<String> names = loadFile("/name/女性");
            return names.get(randomAB(0, names.size()));
        }
        return secondName((int) (Math.random() * 1) == 1 ? "boy" : "girl");
    }

    /**
     * 读取百家姓文件，获取名字
     *
     * @return
     * @throws IOException
     */
//    private static List<String> loadNames(String path) throws IOException {
//        InputStream in = RandomName.class.getResourceAsStream(path);
//        BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
//        //文件读取
//        String line = null;
//        //结果集合
//        List<String> result = new ArrayList<>(200);
//        while ((line = br.readLine()) != null) {
//            line = line.trim();
//            //使用空白字符分割
//            String[] names = line.split("\\s+");
//            result.addAll(Arrays.asList(names));
//        }
//        return result;
//    }

    /**
     * 读取百家姓文件，获取名字
     *
     * @return
     * @throws IOException
     */
    private static List<String> loadFile(String path) throws IOException {
        InputStream in = RandomName.class.getResourceAsStream(path);
        BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
        //文件读取
        String line = null;
        //结果集合
        List<String> result = new ArrayList<>(200);
        while ((line = br.readLine()) != null) {
            line = line.trim();
            //使用空白字符分割
            String[] names = line.split("\\s+");
            result.addAll(Arrays.asList(names));
        }
        return result;
    }
}
